<template>
  <div class="menu-management">
    <el-card shadow="hover">
      <template #header>
        <div class="card-header">
          <span>菜单管理</span>
          <el-button type="primary" @click="handleAdd">添加菜单</el-button>
        </div>
      </template>

      <el-table
          :data="menuTree"
          row-key="id"
          border
          default-expand-all
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          style="width: 100%"
      >
        <el-table-column prop="name" label="菜单名称" width="180" />
<!--        <el-table-column prop="path" label="访问路径" />-->
        <el-table-column prop="level" label="级别" width="200">
          <template #default="{ row }">
            <el-tag :type="row.level === '1' ? 'primary' : 'success'">
              {{ row.level === '1' ? '一级菜单' : '二级菜单' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="访问路径">
          <el-input v-model="form.path" />
        </el-form-item>
        <el-form-item label="父级菜单">
          <el-select v-model="form.parentid" placeholder="请选择" clearable>
            <el-option
                v-for="item in parentMenus"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const menuTree = ref([])
const dialogVisible = ref(false)
const form = ref({
  id: null,
  name: '',
  path: '',
  parentid: 0,
  level: '1'
})
const isEdit = ref(false)

const dialogTitle = computed(() => (isEdit.value ? '编辑菜单' : '添加菜单'))

const parentMenus = computed(() => {
  return menuTree.value.filter(menu => menu.level === '1')
})

const fetchMenuTree = async () => {
  try {
    const response = await axios.get('api/menu/menuUser/tree')
    menuTree.value = response.data
  } catch (error) {
    ElMessage.error('获取菜单失败')
  }
}

const handleAdd = () => {
  form.value = {
    id: null,
    name: '',
    path: '',
    parentid: 0,
    level: '1'
  }
  isEdit.value = false
  dialogVisible.value = true
}

const handleEdit = (row) => {
  form.value = { ...row }
  isEdit.value = true
  dialogVisible.value = true
}

const handleDelete = (id) => {
  ElMessageBox.confirm('确认删除该菜单吗?', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await axios.delete(`api/menu/menuUser/${id}`)
      ElMessage.success('删除成功')
      fetchMenuTree()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

const submitForm = async () => {
  try {
    if (form.value.parentid) {
      form.value.level = '2'
    } else {
      form.value.level = '1'
    }

    if (isEdit.value) {
      await axios.put('api/menu/menuUser', form.value)
      ElMessage.success('更新成功')
    } else {
      await axios.post('api/menu/menuUser', form.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    fetchMenuTree()
  } catch (error) {
    ElMessage.error(isEdit.value ? '更新失败' : '添加失败')
  }
}

onMounted(() => {
  fetchMenuTree()
})
</script>

<style scoped>
.menu-management {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
